﻿using System;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using DBUtility;

namespace OracleDAL.SUBSYSTEM
{
    /// <summary>
    /// 数据访问类SUBSYS_MODULE。
    /// </summary>
    public class SUBSYS_MODULE
    {
        /// <summary>
        /// 
        /// </summary>
        public SUBSYS_MODULE()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string SUBSYSTEMID, string MODULEID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SUBSYS_MODULE");
            strSql.Append(" where SUBSYSTEMID=:SUBSYSTEMID and MODULEID=:MODULEID ");
            OracleParameter[] parameters = {
					new OracleParameter(":SUBSYSTEMID", OracleType.VarChar,50),
					new OracleParameter(":MODULEID", OracleType.VarChar,50)};
            parameters[0].Value = SUBSYSTEMID;
            parameters[1].Value = MODULEID;

            return OracleHelper.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Model.SUBSYSTEM.SUBSYS_MODULE model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SUBSYS_MODULE(");
            strSql.Append("SUBSYSTEMID,MODULEID,MODULENAME,MODULERUNSTATE,COLLECTIONTIME,REMARK)");
            strSql.Append(" values (");
            strSql.Append(":SUBSYSTEMID,:MODULEID,:MODULENAME,:MODULERUNSTATE,:COLLECTIONTIME,:REMARK)");
            OracleParameter[] parameters = {
					new OracleParameter(":SUBSYSTEMID", OracleType.VarChar,25),
					new OracleParameter(":MODULEID", OracleType.VarChar,25),
					new OracleParameter(":MODULENAME", OracleType.VarChar,100),
					new OracleParameter(":MODULERUNSTATE", OracleType.VarChar,2),
					new OracleParameter(":COLLECTIONTIME", OracleType.DateTime),
					new OracleParameter(":REMARK", OracleType.VarChar,1000)};
            parameters[0].Value = model.SUBSYSTEMID;
            parameters[1].Value = model.MODULEID;
            parameters[2].Value = model.MODULENAME;
            parameters[3].Value = model.MODULERUNSTATE;
            parameters[4].Value = model.COLLECTIONTIME;
            parameters[5].Value = model.REMARK;

            OracleHelper.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Model.SUBSYSTEM.SUBSYS_MODULE model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SUBSYS_MODULE set ");
            strSql.Append("MODULENAME=:MODULENAME,");
            strSql.Append("MODULERUNSTATE=:MODULERUNSTATE,");
            strSql.Append("COLLECTIONTIME=:COLLECTIONTIME,");
            strSql.Append("REMARK=:REMARK");
            strSql.Append(" where SUBSYSTEMID=:SUBSYSTEMID and MODULEID=:MODULEID ");
            OracleParameter[] parameters = {
					new OracleParameter(":SUBSYSTEMID", OracleType.VarChar,25),
					new OracleParameter(":MODULEID", OracleType.VarChar,25),
					new OracleParameter(":MODULENAME", OracleType.VarChar,100),
					new OracleParameter(":MODULERUNSTATE", OracleType.VarChar,2),
					new OracleParameter(":COLLECTIONTIME", OracleType.DateTime),
					new OracleParameter(":REMARK", OracleType.VarChar,1000)};
            parameters[0].Value = model.SUBSYSTEMID;
            parameters[1].Value = model.MODULEID;
            parameters[2].Value = model.MODULENAME;
            parameters[3].Value = model.MODULERUNSTATE;
            parameters[4].Value = model.COLLECTIONTIME;
            parameters[5].Value = model.REMARK;

            OracleHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string SUBSYSTEMID, string MODULEID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SUBSYS_MODULE ");
            strSql.Append(" where SUBSYSTEMID=:SUBSYSTEMID and MODULEID=:MODULEID ");
            OracleParameter[] parameters = {
					new OracleParameter(":SUBSYSTEMID", OracleType.VarChar,50),
					new OracleParameter(":MODULEID", OracleType.VarChar,50)};
            parameters[0].Value = SUBSYSTEMID;
            parameters[1].Value = MODULEID;

            OracleHelper.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SUBSYSTEM.SUBSYS_MODULE GetModel(string SUBSYSTEMID, string MODULEID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CODE.CODEVALUE AS SUBSYSTEMNAME,SM.SUBSYSTEMID,SM.MODULEID,SM.MODULENAME,SM.MODULERUNSTATE,SM.COLLECTIONTIME,SM.REMARK ");
            strSql.Append(" from CODE,SUBSYS_MODULE SM  ");
            strSql.Append(" where SM.SUBSYSTEMID=:SUBSYSTEMID and MODULEID=:MODULEID and CODE.CODEID=SM.SUBSYSTEMID ");
            OracleParameter[] parameters = {
					new OracleParameter(":SUBSYSTEMID", OracleType.VarChar,50),
					new OracleParameter(":MODULEID", OracleType.VarChar,50)};
            parameters[0].Value = SUBSYSTEMID;
            parameters[1].Value = MODULEID;

            Model.SUBSYSTEM.SUBSYS_MODULE model = new Model.SUBSYSTEM.SUBSYS_MODULE();
            DataSet ds = OracleHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.SUBSYSTEMID = ds.Tables[0].Rows[0]["SUBSYSTEMID"].ToString();
                model.SUBSYSTEMNAME = ds.Tables[0].Rows[0]["SUBSYSTEMNAME"].ToString();
                model.MODULEID = ds.Tables[0].Rows[0]["MODULEID"].ToString();
                model.MODULENAME = ds.Tables[0].Rows[0]["MODULENAME"].ToString();
                model.MODULERUNSTATE = ds.Tables[0].Rows[0]["MODULERUNSTATE"].ToString();
                if (ds.Tables[0].Rows[0]["COLLECTIONTIME"].ToString() != "")
                {
                    model.COLLECTIONTIME = DateTime.Parse(ds.Tables[0].Rows[0]["COLLECTIONTIME"].ToString());
                }
                model.REMARK = ds.Tables[0].Rows[0]["REMARK"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CODE.CODEID AS SUBSYSTEMID,CODE.CODEVALUE AS SUBSYSTEMNAME,SM.MODULEID,SM.MODULENAME,SM.MODULERUNSTATE,SM.COLLECTIONTIME,SM.REMARK ");
            strSql.Append(" FROM CODE,SUBSYS_MODULE SM  ");
            strSql.Append(" where CODE.CODEID=SM.SUBSYSTEMID  ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" and " + strWhere);
            }
            strSql.Append("  order by CODE.CODEID,SM.MODULENAME ");
            return OracleHelper.Query(strSql.ToString());
        }
         


          /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetModuleList(string SUBSYSTEMID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select SM.MODULENAME,SM.MODULEID,SM.MODULERUNSTATE,SM.COLLECTIONTIME,SM.REMARK, SI.SUBSYSTEMID,SI.DIRECTION,SI.SHORTHAND AS SUBSYSTEMNAME ");
            strSql.Append(" from SUBSYS_MODULE SM,SUBSYS_INFO SI   ");
            strSql.Append(" where SM.SUBSYSTEMID(+)=SI.SUBSYSTEMID and SI.ISDISPLAY='Y' and SI.DIRECTION<=12  ");
            if (SUBSYSTEMID.Trim().Length > 0)
            {
                strSql.Append(" and SI.SUBSYSTEMID='" + SUBSYSTEMID + "'");
            }
            strSql.Append("  order by SI.SUBSYSTEMID,SM.MODULENAME ");
            return OracleHelper.Query(strSql.ToString());
        }
        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            OracleParameter[] parameters = {
                    new OracleParameter(":tblName", OracleType.VarChar, 255),
                    new OracleParameter(":fldName", OracleType.VarChar, 255),
                    new OracleParameter(":PageSize", OracleType.Number),
                    new OracleParameter(":PageIndex", OracleType.Number),
                    new OracleParameter(":IsReCount", OracleType.Clob),
                    new OracleParameter(":OrderType", OracleType.Clob),
                    new OracleParameter(":strWhere", OracleType.VarChar,1000),
                    };
            parameters[0].Value = "SUBSYS_MODULE";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return OracleHelper.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}

